<?php


namespace app\api\service;


use think\Db;

/**
 * 药店提现绑银行卡
 * Class PharmacyBankAccountService
 * @package app\api\service\
 */
class PharmacyBankAccountService
{

    /**
     * 新增药店绑卡
     * @param $pharmacyId
     * @param $bankAccount
     * @param $bankAccountName
     * @param $bankName
     * @return bool
     */
    public function addBankAccount($pharmacyId, $bankAccount, $bankAccountName, $bankName){

        $sql = "insert into jn_pharmacy_bank_account(pharmacy_id, bank_name, bank_account, bank_account_name, created_at)
                value (?, ?, ?, ?, now())";

        $result = Db::execute($sql, [$pharmacyId, $bankName, $bankAccount, $bankAccountName]);

        return $result > 0;

    }

    /**
     * 获取药店绑定的银行卡
     * @param $pharmacyId
     * @return mixed
     */
    public function fetchBankAccount($pharmacyId){

        $sql = "select pharmacy_id, bank_name, bank_account, bank_account_name from jn_pharmacy_bank_account where pharmacy_id = ?";

        $data = Db::query($sql, [$pharmacyId]);

        return $data;

    }


    /**
     *  申请提现
     */
    public function addWithdraw($pharmacyId, $bankAccountId,$amount){

        $sql = "insert into jn_pharmacy_withdraw(pharmacy_id, bank_account_id, amount, created_at) value (?, ?, ?, now());";

        $r = Db::execute($sql, [$pharmacyId, $bankAccountId, $amount]);

        return $r > 0;

    }

    /**
     * 获取可提现金额
     * @param $pharmacyId
     * @return int
     */
    public function getCanWithdrawAmount($pharmacyId){

        $sql = "select * from jn_pharmacy_wallet where pharmacy_id = ? limit 1";

        $data = Db::query($sql, [$pharmacyId]);

        if(empty($data)){
            return 0;
        }

        return $data['amount'];

    }


}